package com.ThreadStudy.StudyDemo;

public class RaceRabbitSleep implements Runnable{

    String winner;

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {

        // 兔子睡觉
            if(Thread.currentThread().getName().equals("兔子") && i%10==0){
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(Thread.currentThread().getName()+"跑了"+i+"米");

            Boolean flag = RaceOver(i);

            if(flag == true){
                break;
            }

        }
    }



    // 如果是true就是退出,如果是false就是停止
    public Boolean RaceOver(int steps){

        if(steps==100){
            winner = Thread.currentThread().getName();
            System.out.println("胜利者:"+winner);
            return true;
        }

        // 比赛结束,关闭所有
        if(winner!=null){
            return true;
        }


        return false;
    }


    public static void main(String[] args) {
        RaceRabbitSleep raceRabbitSleep = new RaceRabbitSleep();
        Thread thread = new Thread(raceRabbitSleep,"兔子");
        Thread thread1 = new Thread(raceRabbitSleep,"乌龟");


        thread.start();
        thread1.start();







    }
}
